package benchmark;

import java.util.*;
import java.util.concurrent.*;

public abstract class AbstractMTTask implements ITask {

	private List<Callable<String>> callables;
	private List<Future<String>> results;

	protected int threadNum;
		
	@Override
	public void init(List<String> param) {
		threadNum = Integer.parseInt(param.get(1));
		callables = initCallables(param);
		results = new ArrayList<Future<String>>();
	}
	
	protected abstract List<Callable<String>> initCallables(List<String> param);

	@Override
	public String runTask() {
		ExecutorService executor = Executors.newFixedThreadPool(threadNum + 1);
		for (Callable<String> c : callables) {
			results.add(executor.submit(c));
		}
		executor.shutdown();
		while (!executor.isTerminated()) {
			// TODO: need sleep?
		}
		
		String result = "";
		try {
			for (Future<String> f : results) {
				result += f.get() + ",";
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}


}
